﻿using AutoMapper;
using BCGM.MedicalWisdom.IServices.Orders;
using BCGM.MedicalWisdom.Model;
using BCGM.MedicalWisdom.Model.Models;
using BCGM.MedicalWisdom.Model.ViewModels.Order;
using BCGM.MedicalWisdom.Repository.BASE;
using BCGM.MedicalWisdom.Services.BASE;
using CSRedis;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace BCGM.MedicalWisdom.Services.Orders
{
    public class DrugclassifyServices : BaseServices<Drugclassify>, IDrugclassifyServices
    {
        private readonly IBaseRepository<Drugclassify> _dal;
        private readonly IConfiguration _configuration;
        private readonly IMapper _mapper;
        public DrugclassifyServices(IBaseRepository<Drugclassify> dal,  IConfiguration configuration, IMapper mapper)
        {
            this._dal = dal;
            base.BaseDal = dal;
            _configuration = configuration;
            _mapper = mapper;
        }
        //public async Task<PageModel<DrugclassifyDto>> GetDrugOrder( string orderByFields = null)
        //{
        //    List<Drugclassify> drugOrder = await _dal.Query();
        //    DrugclassifyDto drugOrderDto = _mapper.Map<DrugclassifyDto>(drugOrder);
        //    //返回表达式
        //    Expression<Func<Drugclassify, DrugclassifyDto>>
        //        selectExpression = Drugclassify => drugOrderDto;
        //    //查询表达式
        //    Expression<Func<DrugclassifyDto, bool>> whereExpression = Drugclassify => Drugclassify.Pharmaceuticalsfenzhuang == false;
        //    var list = await base.BaseDal.QueryTabsPage
        //        (
        //         selectExpression, whereExpression, orderByFields
        //        );
        //    return list;
        //}
        /// <summary>
        /// sql语句药品订单详细显示+查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<DrugclassifyDto>> GetDrugclassifies(string ClassIfyName)
        {
            //string sql = $"select * from Drugclassify where ClassIfyName='{ClassIfyName}';";
            string sql = "select * from Drugclassify where Pharmaceuticalsfenzhuang = 0;";
            var list = await base.BaseDal.QuerySql(sql);
            var drugOrders = _mapper.Map<List<DrugclassifyDto>>(list);
            return drugOrders;
        } 
        /// <summary>
        /// 药品添加购物车
        /// </summary>
        /// <param name="carDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
       public bool AddDrugRedis(DrugclassifyDto drugclassifyDto)
        {
            CSRedisClient cSRedis = new(_configuration.GetSection("Redis").Value);
            List<DrugclassifyDto> drugclassify = new();
            //查询Redis HGet是否存在已添加的数据
            drugclassify = cSRedis.HGet<List<DrugclassifyDto>>("Drugclassify", drugclassifyDto.ClassIfyName);
            //判断Redis存储购物车是否有数据 无 则新实例化hk
            drugclassify ??= new();
            //查询新增商品与购物车存在商品是否一致，如一致则进行商品数量的添加
            var oldDrug = drugclassify.Where(x => x.ClassIfyName.Equals(drugclassifyDto.ClassIfyName)).FirstOrDefault();
            if (oldDrug != null)
            {
                drugclassify.Remove(oldDrug);
                drugclassifyDto.ClassIfyNum += oldDrug.ClassIfyNum;
            }
            drugclassify.Add(drugclassifyDto);
            return cSRedis.HSet("Drugclassify", drugclassifyDto.ClassIfyName, drugclassify);
        }

        public List<DrugclassifyDto> QueryByname(string ClassIfyName)
        {
            CSRedisClient cSRedis = new(_configuration.GetSection("Redis").Value);
            List<DrugclassifyDto> drugsCar = cSRedis.HGet<List<DrugclassifyDto>>("Drugclassify", ClassIfyName);
            return drugsCar;
        }

        /// <summary>
        /// 获取药品购物车数据
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        //public List<DrugclassifyDto> GetDrugCarRedis(string ClassIfyName)
        //{
        //        CSRedisClient cSRedis = new(_configuration.GetSection("Redis").Value);
        //        List<DrugclassifyDto> drugsCar = cSRedis.HGet<List<DrugclassifyDto>>("Drugclassify", ClassIfyName);
        //        return drugsCar;
        //}

        //public Task<DrugclassifyDto> QueryByname(string classIfyName)
        //{
        //    CSRedisClient cSRedis = new(_configuration.GetSection("Redis").Value);
        //    List<DrugclassifyDto> drugsCar = cSRedis.HGet<List<DrugclassifyDto>>("Drugclassify", ClassIfyName);
        //    return drugsCar;
        //}
    }
}
